package com.weijianhuawen.wjblog.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.weijianhuawen.wjblog.common.domain.dos.ArticleDo;
import com.weijianhuawen.wjblog.common.domain.mapper.ArticleMapper;
import com.weijianhuawen.wjblog.common.utils.PageResponse;
import com.weijianhuawen.wjblog.web.convert.ArticleConvert;
import com.weijianhuawen.wjblog.web.mode.vo.archive.FindArchiveArticlePageListReqVo;
import com.weijianhuawen.wjblog.web.mode.vo.archive.FindArchiveArticlePageListRspVo;
import com.weijianhuawen.wjblog.web.mode.vo.archive.FindArchiveArticleRspVo;
import com.weijianhuawen.wjblog.web.service.ArchiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.YearMonth;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-29 23:16
 */

@Service
@Slf4j
public class ArchiveServiceImpl implements ArchiveService {
    @Resource
    private ArticleMapper articleMapper;

    // 获取文章归档分页数据
    @Override
    public PageResponse findArchivePageList(FindArchiveArticlePageListReqVo vo) {
        // 获取请求的页码，与数量
        Long current = vo.getCurrent();
        Long size = vo.getSize();

        // 分页查询
        IPage<ArticleDo> page = articleMapper.selectPageList(current, size, null, null, null, null);
        // 获取分页查询数据
        List<ArticleDo> articleDoList = page.getRecords();

        // do 转 vo
        List<FindArchiveArticlePageListRspVo> vos = Lists.newArrayList();

        if (!CollectionUtils.isEmpty(articleDoList)) {
            List<FindArchiveArticleRspVo> findArchiveArticleRspVos =
                    articleDoList.stream()
                            .map(articleDo -> ArticleConvert.INSTENCE.convertDoToArchiveVo(articleDo))
                            .collect(Collectors.toList());

            // 将数据按照月份进行分组
            Map<YearMonth, List<FindArchiveArticleRspVo>> monthListMap = findArchiveArticleRspVos
                    .stream()
                    .collect(Collectors.groupingBy(FindArchiveArticleRspVo::getCreateMonth));

            // 使用TreeMap将月份倒序排序
            Map<YearMonth, List<FindArchiveArticleRspVo>> treeMap = new TreeMap<>((a, b) -> {
                if (a.getYear() == b.getYear()) {
                    return b.getMonthValue() - a.getMonthValue();
                } else return b.getYear() - a.getYear();
            });
            //Collections.reverseOrder();
            treeMap.putAll(monthListMap);

            // 遍历排序后的map， 将其归档为vo
            treeMap.forEach((k, v) -> vos.add(FindArchiveArticlePageListRspVo
                    .builder()
                            .articles(v)
                            .yearMonth(k)
                    .build()));
        }
        return PageResponse.success(page, vos);
    }
}
